package com.bos.service.impl;

import com.bos.data.model.ProjectItemHistoryModel;
import com.bos.data.model.ProjectItemModel;
import com.bos.data.model.ProjectPropertyHistoryModel;
import com.bos.data.model.ProjectPropertyModel;
import com.bos.data.repositories.jpa.BosProjectItemJPARepository;
import com.bos.data.repositories.jpa.ProjectItemHistoryJPARepository;
import com.bos.data.repositories.jpa.ProjectPropertyHistoryJPA;
import com.bos.mapper.Project_itemMapper;
import com.bos.service.Project_itemSerice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("itemServiceImpl")
public class Project_itemServiceImpl implements Project_itemSerice {
    private Logger logger = LoggerFactory.getLogger(Project_itemServiceImpl.class);
    @Autowired
    Project_itemMapper itemMapper;

    @Autowired
    private BosProjectItemJPARepository bosProjectItemJPARepository;

    @Autowired
    private ProjectItemHistoryJPARepository projectItemHistoryJPARepository;

    @Autowired
    private ProjectPropertyHistoryJPA projectPropertyHistoryJPA;

    @Override
    public int insertProperty(ProjectPropertyModel pro) {
        return itemMapper.insertProperty(pro);
    }

    @Transactional
    @Override
    public void insertItem(List<ProjectItemModel> item) {
        try {
            bosProjectItemJPARepository.save(item);
        }catch (Exception e){
            logger.error("字段插入到project_item中失败",e);
        }
    }

    @Override
    public ProjectPropertyModel findPropertyByProp(String prop) {
        // TODO Auto-generated method stub
        return itemMapper.findPropertyByProp(prop);
    }

    @Override
    public List<ProjectItemModel> findByProid(int proId) {
        return itemMapper.findByProid(proId);
    }

    @Override
    public List<ProjectItemModel> findAll() {
        return itemMapper.findAll();
    }

    @Transactional
    @Override
    public void updProItem(int id, String value) {
        try {
            bosProjectItemJPARepository.updProItem(id, value);
        } catch (Exception e) {
            logger.error("更新project_item表报错");
        }
    }

    @Transactional
    @Override
    public void addProItem(ProjectItemModel projectItemModel) {
        try {
            bosProjectItemJPARepository.save(projectItemModel);
        } catch (Exception e) {
            logger.error("添加project_item表数据失败",e);
        }
    }

    @Transactional
    @Override
    public int delByProitid(int proitid) {
        try {
            return bosProjectItemJPARepository.delByProitid(proitid);
        } catch (Exception e) {
            logger.error("删除字段失败",e);
            return 0;
        }
    }

    @Transactional
    @Override
    public void addProItemHistory(ProjectItemHistoryModel historyItem){
        try {
            projectItemHistoryJPARepository.save(historyItem);
        }catch (Exception e){
            logger.error("添加project_item_history表数据失败",e);
        }
    }

    @Transactional
    @Override
    public int addProProHistory(ProjectPropertyHistoryModel model){
        try {
            return projectPropertyHistoryJPA.saveAndFlush(model).getId();
        }catch (Exception e){
            logger.error("添加project_property_history表数据失败",e);
            return 0;
        }
    }

    @Transactional
    @Override
    public void insertItemHistory(List<ProjectItemHistoryModel> item){
        try {
            projectItemHistoryJPARepository.save(item);
        }catch (Exception e){
            logger.error("字段插入到project_item_history中失败",e);
        }
    }

    @Override
    public List<ProjectItemHistoryModel> findByPid(int pid){
        return projectItemHistoryJPARepository.findByProid(pid);
    }

    @Transactional
    @Override
    public int delHistoryByProitid(int proitid){
        try {
            return projectItemHistoryJPARepository.delHistoryByProitid(proitid);
        }catch (Exception e){
            logger.error("删除字段历史表失败",e);
            return 0;
        }

    }

    @Transactional
    @Override
    public void updProItemHistory(int id, String value){
        projectItemHistoryJPARepository.updProItemHistory(id, value);
    }
}
